//
//  YLCInviteCompanyIntroCell.m
//  LinYa
//
//  Created by 初程程 on 2018/2/23.
//  Copyright © 2018年 初程程. All rights reserved.
//

#import "YLCInviteCompanyIntroCell.h"
#import "YLCPhotoCell.h"
#import "SDPhotoBrowser.h"

#define PHOTO_CELL  @"YLCPhotoCell"

@interface YLCInviteCompanyIntroCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,SDPhotoBrowserDelegate>
@end

@implementation YLCInviteCompanyIntroCell
{
    UILabel *companyNameLabel;
    UILabel *introLabel;
    UILabel *areaLabel;
    UICollectionView *imageTable;
//    UIImageView *arrowView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self createCustomView];
    }
    return self;
}
- (void)createCustomView{
    companyNameLabel = [YLCFactory createLabelWithFont:16 color:RGB(30, 30, 30)];
    
    [self.contentView addSubview:companyNameLabel];
    
    introLabel = [YLCFactory createLabelWithFont:14 color:RGB(183, 183, 183)];
    
    [self.contentView addSubview:introLabel];
    
    areaLabel = [YLCFactory createLabelWithFont:14 color:RGB(183, 183, 183)];
    
    [self.contentView addSubview:areaLabel];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    
    flow.minimumLineSpacing = 5;
    
    flow.minimumInteritemSpacing = 5;
    
    imageTable = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flow];
    
    [imageTable registerClass:[YLCPhotoCell class] forCellWithReuseIdentifier:PHOTO_CELL];
    
    imageTable.delegate = self;
    
    imageTable.dataSource = self;
    
    imageTable.bounces = NO;
    
    imageTable.alwaysBounceVertical = NO;
    
    imageTable.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:imageTable];
    
//    arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"resume_arrow"]];
//    
//    [self.contentView addSubview:arrowView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([_model.imglist isKindOfClass:[NSNull class]]) {
        return 0;
    }
    return _model.imglist.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YLCPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PHOTO_CELL forIndexPath:indexPath];
    
    if (indexPath.row<_model.imglist.count) {
        [cell configImage:_model.imglist[indexPath.row]];
    }
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((self.frameWidth-30-20)/3, (self.frameWidth-30-20)/3);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new];
    photoBrowser.delegate = self;
    photoBrowser.currentImageIndex = indexPath.row;
    photoBrowser.imageCount = _model.imglist.count;
    photoBrowser.sourceImagesContainerView = collectionView;
    photoBrowser.isShowSaveButton = YES;
    [photoBrowser show];
}
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index{
    return [NSURL URLWithString:_model.imglist[index]];
}
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
    return nil;
}
- (void)setModel:(YLCInviteModel *)model{
    _model = model;
    companyNameLabel.text = [model.company_name notNullString];
    
//    introLabel.text = [NSString stringWithFormat:@"%@ | %@",[model.company_size notNullString],model.type];
    
    areaLabel.text = [model.place notNullString];
    
    [imageTable reloadData];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    WS(weakSelf)
    [companyNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.top.equalTo(weakSelf.mas_top).offset(10);
    }];
    
    [introLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(companyNameLabel.mas_left);
        make.top.equalTo(companyNameLabel.mas_bottom).offset(5);
    }];
    
    [areaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(introLabel.mas_left);
        make.top.equalTo(introLabel.mas_bottom).offset(5);
    }];
    
    [imageTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(areaLabel.mas_left);
        make.right.equalTo(self.mas_right).offset(-15);
        make.top.equalTo(areaLabel.mas_bottom).offset(10);
        make.bottom.equalTo(self.mas_bottom).offset(-10);
    }];
    
//    [arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(weakSelf.mas_right).offset(-10);
//        make.centerY.equalTo(weakSelf.mas_centerY);
//        make.width.equalTo(7);
//        make.height.equalTo(12);
//    }];
}
@end
